package com.divine.wn.car.action;

import java.math.BigDecimal;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.divine.wn.car.service.BackUserService;
import com.divine.wn.car.service.BaseDataService;
import com.divine.wn.car.service.LogService;
import com.divine.wn.car.service.UserService;
import com.divine.wn.car.service.WorkflowService;
import com.divine.wn.car.vo.BackUserVO;

public class BaseAction extends DispatchAction {
	protected static HttpSession session;
	protected static ServletContext sc;
	protected static ApplicationContext ac;
	
	protected static BackUserService backUserService;
	protected static BaseDataService baseDataService;
	protected static LogService logService;
	protected static WorkflowService workflowService;
	protected static UserService userService;
	BackUserVO backUser;
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		session = request.getSession();
		sc = session.getServletContext();
		ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		userService = (UserService) (ac.getBean("userService"));
		baseDataService = (BaseDataService) ac.getBean("baseDataService");
		backUserService = (BackUserService) ac.getBean("backUserService");
		logService = (LogService) ac.getBean("logService");
		workflowService = (WorkflowService) ac.getBean("workflowService");
		backUser = (BackUserVO) session.getAttribute("buser");
		
		return super.execute(mapping, form, request, response);
	}

	private String getRate(String aRate){
		
		int x=aRate.indexOf("_");		
		
		return aRate.substring(x+1);
	}
	
	//x*y*0.01  保留两位小数
	protected String changeData(String x,String y){
		y=getRate(y);
		return (new BigDecimal(x).multiply(new BigDecimal(y)).multiply(new BigDecimal(0.01))).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
	}
}
